package com.cwnu.mapper;

import com.cwnu.entity.Article;
import com.cwnu.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleMapper {

    @Insert("insert into article(title, content, summary, create_id, category_id, create_time, update_time, status)" +
            " values (#{title},#{content},#{summary},#{createId},#{categoryId},#{createTime},#{updateTime}, 0);")
    void add(Article article);


    @Select("select * from article where id=#{id};")
    Article findById(Integer id);


    void update(Article article);

    @Delete("delete from article where id=#{id}")
    void delete(Integer id);

    List<Article> list(Integer userId, Integer categoryId);

    List<Article> listAll(Integer categoryId, Integer status);

    @Select("select * from article where create_id = #{userId}")
    List<Article> listWithoutPb(Integer userId);

    @Select("select * from user where id = (" +
            "select create_id from article where id = #{articleId})")
    User getUserByArticleId(Integer articleId);

    @Select("select * from article where (status = 0 or status = 3) and create_id = #{userId}")
    List<Article> getReviewArticle(Integer userId);

    @Select("select * from article \n" +
            "where title like concat('%', #{keyword}, '%') \n" +
            "   or content like concat('%', #{keyword}, '%') \n" +
            "   or summary like concat('%', #{keyword}, '%') and status = 2;")
    List<Article> search(String key);
}
